package cn.lingyangwl.agile.misc.short_link.controller;

import cn.lingyangwl.agile.infra.*;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.*;
import cn.lingyangwl.agile.misc.short_link.model.entity.*;
import cn.lingyangwl.agile.misc.short_link.model.rqrs.*;
import cn.lingyangwl.agile.misc.short_link.service.*;
import cn.lingyangwl.agile.model.enums.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 短链
 *
 * @author shenguangyang
 */
@Api(tags = "短链")
@RestController
@RequestMapping("/shortLink")
public class ShortLinkController {
    @Resource
    private ShortLinkService shortLinkService;

    @Resource
    private InfraApi infraApi;

    @ApiOperation(value = "保存短链组")
    @PostMapping("/saveGroup")
    @RamResource(action = "sl:shortLink:saveGroup", name = "保存短链组", type = ActionType.WRITE)
    public Resp<?> saveDataGroup(@RequestBody DataGroupSaveReq req) {
        req.setBizType(DataGroupBizTypeEnum.SHORT_LINK.getType());
        return Resp.ok(infraApi.saveDataGroup(req));
    }

    @ApiOperation(value = "更新短链组")
    @PostMapping("/updateGroup")
    @RamResource(action = "sl:shortLink:updateGroup", name = "更新短链组", type = ActionType.WRITE)
    public Resp<?> updateDataGroup(@RequestBody DataGroupUpdateReq req) {
        return Resp.ok(infraApi.updateDataGroup(req));
    }

    @ApiOperation(value = "批量删除短链组")
    @DeleteMapping("/batchDeleteGroup")
    @RamResource(action = "sl:shortLink:deleteGroup", name = "删除短链组", type = ActionType.WRITE)
    public Resp<?> batchDeleteDataGroup(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(shortLinkService.batchDeleteGroup(ids));
    }

    @ApiOperation(value = "删除短链组")
    @DeleteMapping("/deleteGroup")
    @RamResource(action = "sl:shortLink:deleteGroup", name = "删除短链组", type = ActionType.WRITE)
    public Resp<?> deleteDataGroup(@RequestParam(value = "id") Long id) {
        return Resp.ok(shortLinkService.batchDeleteGroup(Collections.singletonList(id)));
    }

    @ApiOperation(value = "保存-短链")
    @PostMapping("/saveLink")
    @RamResource(action = "sl:shortLink:save", name = "保存短链", type = ActionType.WRITE)
    public Resp<?> saveLink(@Validated @RequestBody ShortLinkSaveReq req) {
        return Resp.ok(shortLinkService.saveLink(req));
    }

    @ApiOperation(value = "分页-短链")
    @GetMapping("/pageLink")
    @RamResource(action = "sl:shortLink:query", name = "查询短链", type = ActionType.READ)
    public Resp<IPage<ShortLinkDto>> pageLink(@Validated ShortLinkPageReq req) {
        return Resp.ok(shortLinkService.pageLink(req));
    }

    @ApiOperation(value = "通过id查找-短链")
    @GetMapping("/getLink")
    @RamResource(action = "sl:shortLink:query", name = "查询短链", type = ActionType.READ)
    public Resp<ShortLink> getLink(@RequestParam(value = "id") Long id, @RequestParam("code") String code) {
        return Resp.ok(shortLinkService.getLinkById(id, code));
    }

    @ApiOperation(value = "更新-短链")
    @PostMapping("/updateLink")
    @RamResource(action = "sl:shortLink:update", name = "更新短链", type = ActionType.WRITE)
    public Resp<?> updateLink(@Validated @RequestBody ShortLinkUpdateReq req) {
        return Resp.ok(shortLinkService.updateLink(req));
    }

    @ApiOperation(value = "批量删除-短链")
    @RamResource(action = "sl:shortLink:delete", name = "删除短链", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteLink")
    public Resp<?> batchDeleteLink(@Validated ShortLinkDeleteReq req) {
        return Resp.ok(shortLinkService.deleteLinkByIds(req));
    }
}
